package com.store.pet.petstore.activity;

import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.droidbyme.dialoglib.DroidDialog;
import com.gyf.barlibrary.ImmersionBar;
import com.squareup.picasso.Picasso;
import com.store.pet.petstore.R;
import com.store.pet.petstore.bean.PetListBean;

import butterknife.BindView;
import butterknife.OnClick;

import static com.store.pet.petstore.activity.PetListActivity.BUY_SUCCESS;

/**
 * 宠物详情页
 */
public class PetDetailActivity extends BaseActivity {

    @BindView(R.id.my_status)
    View myStatus;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.pet_img)
    ImageView petImg;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.birthday)
    TextView birthday;
    @BindView(R.id.describe)
    TextView describe;
    @BindView(R.id.buy)
    Button buy;
    @BindView(R.id.function)
    RelativeLayout function;

    private PetListBean.ContentBean.PetlistBean data;  //从上一个界面传过来的数据

    private DroidDialog dialog;  //拨打电话的弹窗



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int getLayoutID() {
        return R.layout.activity_pet_detail;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        //沉浸式标题栏
        ImmersionBar.with(this).statusBarDarkFont(true).init();
        takeRelativeStatusBar(myStatus);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationIcon(R.drawable.fanhui);  //设置左上角图标
        toolbar.setNavigationOnClickListener(view -> finish());  //退出
    }

    @Override
    public void loadData() {
        if (getIntent().getSerializableExtra("pet") != null) {
            data = (PetListBean.ContentBean.PetlistBean) getIntent().getSerializableExtra("pet");
            Picasso.with(this).load(data.getImg()).into(petImg);
            name.setText(data.getName());
            birthday.setText("生日：" + data.getBirthday());
            describe.setText("描述：" + "\n" + data.getDescription());
            int sellerId = data.getSellerid();
        }
        if (getUserRole() == 2) {
            //卖家不能购买
            function.setVisibility(View.GONE);
        } else if (getUserRole() == 1) {
            function.setVisibility(View.VISIBLE);
        }
    }

    @OnClick({R.id.phone, R.id.buy})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.phone:
                //呼叫卖家电话
                dialog = new DroidDialog.Builder(this)
                        .icon(R.drawable.dianhua)
                        .title("温馨提示")
                        .content("是否确认拨打卖家电话" + data.getSellerphone() + "？")
                        .cancelable(false, false)
                        .positiveButton("确定", droidDialog -> {
                            Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + data.getSellerphone()));
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                            dialog.dismiss();
                        })
                        .negativeButton("取消", droidDialog -> dialog.dismiss()).show();
                break;
            case R.id.buy:
                Intent intent = new Intent(PetDetailActivity.this,EditAddressActivity.class);
                intent.putExtra("buyer",getMyId());
                intent.putExtra("petid",data.getId());
                startActivityForResult(intent,BUY_SUCCESS);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if(data != null){
            switch (requestCode){
                case BUY_SUCCESS:
                    setResult(BUY_SUCCESS,data);
                    finish();
                    break;
            }
        }
    }
}
